package com.example.testapplication.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.example.testapplication.fragment.BookCoverFragment;
import com.example.testapplication.fragment.BookDetailFragment;

import java.util.ArrayList;

public class GoodsPagerAdapter extends FragmentPagerAdapter {
    private ArrayList<String> mTitleArray; // 声明一个标题文字队列

    // 碎片页适配器的构造器函数，传入碎片管理器与标题队列
    public GoodsPagerAdapter(FragmentManager fm,ArrayList<String> titleArray){
        super(fm);
        mTitleArray = titleArray;
    }

    // 获取指定位置的碎片Fragment
    @NonNull
    @Override
    public Fragment getItem(int position) {
        if(position == 0){ // 第一页展示书籍图片
            return new BookCoverFragment();
        }else if(position ==1){  // 第二页展示书籍详情
            return new BookDetailFragment();

        }
        return new BookCoverFragment();
    }

    @Override
    public int getCount() {
        return mTitleArray.size();
    }

    // 获取指定碎片的标题文本
    public CharSequence getPageTitle(int position){
        return mTitleArray.get(position);
    }
}
